﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using ThuVien;

namespace TranTekBE.Pharmacity
{
    public partial class Price_Tag : PageRequireLogin
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "LoadJS", "LoadJS();", true);
            this.Page.Form.DefaultButton = btnSubmit.UniqueID;
        }

        protected void btnSubmit_Mavach_Click(object sender, EventArgs e)
        {
            
            try
            {
                string value_txtMaVach = txtMaVach.Text;
                string DivHtml;
                //
                // Split the string on line breaks.
                // ... The return value from Split is a string array.
                //
                string[] items_MaVach = Regex.Split(value_txtMaVach, "\r\n");
                // check dvt lớn nhất nhỏ nhất
                string check_dvt = droplist_dvt.SelectedValue.ToString();
                string sql = null;
                // check mã POS trên textbox
                string[] items_POS = Regex.Split(value_txtMaVach, ";");
                string Ma_POS_textbox = items_POS[0];
                int check_POS_MaVach = 0;
                if (value_txtMaVach.Contains(";"))
                {
                    check_POS_MaVach = 1;
                }
                else { check_POS_MaVach = 0; }
                //
                for(int i = 0; i < items_MaVach.Length; i ++)
                {
                    if (check_POS_MaVach == 0)
                    {
                        if (check_dvt == "desc")
                        {
                            sql = @"select C001500 as MaChietKhau, C001503 as KhuyenMai, C001508 as KMTuNgay , C001509 as KMDenNgay ,* from T0015 where C001500 = 
                            (select C007501 as MaChietKhau from T0075 where C007502  = 
                            (select C001101 as MaThuocTinh  from T0011 where C001102 = 
                            (select C001200 as ID_SP from T0012 where C001205 = '" + items_MaVach[i] + @"')))

                            select top 1 Sp.C001203 as 'MaSP' ,Sp.C001205 as 'MaVach' ,Sp.C001204 as 'TenSP' , Sp.C001215 as 'DVT' , Gia.C000704 as 'Gia' from T0012 SP
                            Left Join T0007 Gia
                            on SP.C001200 = Gia.C000701
                            where Sp.C001205 = '" + items_MaVach[i] + "' and Gia.C002000 = 3 order by Gia.C000704 desc";
                        }else{
                            sql = @"select C001500 as MaChietKhau, C001503 as KhuyenMai, C001508 as KMTuNgay , C001509 as KMDenNgay ,* from T0015 where C001500 = 
                            (select C007501 as MaChietKhau from T0075 where C007502  = 
                            (select C001101 as MaThuocTinh  from T0011 where C001102 = 
                            (select C001200 as ID_SP from T0012 where C001205 = '" + items_MaVach[i] + @"')))

                            select top 1 Sp.C001203 as 'MaSP' ,Sp.C001205 as 'MaVach' ,Sp.C001204 as 'TenSP' , Sp.C001215 as 'DVT' , Gia.C000704 as 'Gia' from T0012 SP
                            Left Join T0007 Gia
                            on SP.C001200 = Gia.C000701
                            where Sp.C001205 = '" + items_MaVach[i] + "' and Gia.C002000 = 3 order by Gia.C000704 asc";
                        }
                    }else
                    {
                        if (check_dvt == "desc")
                        {
                            sql = @"select C001500 as MaChietKhau, C001503 as KhuyenMai, C001508 as KMTuNgay , C001509 as KMDenNgay ,* from T0015 where C001500 = 
                            (select C007501 as MaChietKhau from T0075 where C007502  = 
                            (select C001101 as MaThuocTinh  from T0011 where C001102 = 
                            (select C001200 as ID_SP from T0012 where C001203 = '" + Ma_POS_textbox + @"')))

                            select top 1 Sp.C001203 as 'MaSP' ,Sp.C001205 as 'MaVach' ,Sp.C001204 as 'TenSP' , Sp.C001215 as 'DVT' , Gia.C000704 as 'Gia' from T0012 SP
                            Left Join T0007 Gia
                            on SP.C001200 = Gia.C000701
                            where Sp.C001203 = '" + Ma_POS_textbox + "' and Gia.C002000 = 3 order by Gia.C000704 desc";
                        }else{
                            sql = @"select C001500 as MaChietKhau, C001503 as KhuyenMai, C001508 as KMTuNgay , C001509 as KMDenNgay ,* from T0015 where C001500 = 
                            (select C007501 as MaChietKhau from T0075 where C007502  = 
                            (select C001101 as MaThuocTinh  from T0011 where C001102 = 
                            (select C001200 as ID_SP from T0012 where C001203 = '" + Ma_POS_textbox + @"')))

                            select top 1 Sp.C001203 as 'MaSP' ,Sp.C001205 as 'MaVach' ,Sp.C001204 as 'TenSP' , Sp.C001215 as 'DVT' , Gia.C000704 as 'Gia' from T0012 SP
                            Left Join T0007 Gia
                            on SP.C001200 = Gia.C000701
                            where Sp.C001203 = '" + Ma_POS_textbox + "' and Gia.C002000 = 3 order by Gia.C000704 asc";
                        }
                    }

                //DataSet ds = FunctionsUtils.Database.ExecuteQuery(
                //@"select top 1 Sp.C001203 as 'MaSP' ,Sp.C001205 as 'MaVach' ,Sp.C001204 as 'TenSP' , Gia.C000704 as 'Gia' from T0012 SP
                //Left Join T0007 Gia
                //on SP.C001200 = Gia.C000701
                //where Sp.C001205 = '"+items_MaVach[i]+"'");
               DataSet ds = FunctionsUtils.Database.ExecuteQuery(sql);


                if (ds != null && ds.Tables.Count > 0 && ds.Tables[1].Rows.Count > 0)
                {
                    if (ViewState["CountItems"] != null)
                        ViewState["CountItems"] = Convert.ToInt32(ViewState["CountItems"]) + 1;
                    else
                        ViewState["CountItems"] = 1;
                    string ImgCode = "http://datamatrix.kaywa.com/img.php?s=8&amp;d=";
                    string MaVach = ds.Tables[1].Rows[0]["MaVach"].ToString();
                    if (MaVach == null || MaVach == "")
                    {
                        MaVach = "/";
                    }
                    string NameSP = ds.Tables[1].Rows[0]["TenSP"].ToString();
                    if (NameSP.Length >= 35)
                        NameSP = NameSP.Substring(0, 35);
                    else
                    {
                        if (NameSP.Length <= 20)
                        {
                            NameSP = NameSP + "</br>";
                        }
                    }
                    string MaPOS = ds.Tables[1].Rows[0]["MaSP"].ToString();
                    double PriceSP = Convert.ToDouble(ds.Tables[1].Rows[0]["Gia"].ToString());
                    

                    string str_StartKM = null;
                    string str_EndKM = null;
                    double PhanTram_KM = 0;
                    DateTime date_StartKM = Convert.ToDateTime("01/01/1999");
                    DateTime date_EndKM = Convert.ToDateTime("01/01/1999");

                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        str_StartKM = ds.Tables[0].Rows[0]["KMTuNgay"].ToString();
                        str_EndKM = ds.Tables[0].Rows[0]["KMDenNgay"].ToString();
                        PhanTram_KM = Convert.ToDouble(ds.Tables[0].Rows[0]["KhuyenMai"].ToString());
                        if (str_StartKM != null)
                        {
                            date_StartKM = Convert.ToDateTime(str_StartKM);
                        }

                        if (str_EndKM != null)
                        {
                            date_EndKM = Convert.ToDateTime(str_EndKM);
                        }
                    }
                    // check khuyến mãi
                    double check_start_KM = (DateTime.Now - date_StartKM).TotalDays;
                    double check_end_KM = (DateTime.Now - date_EndKM).TotalDays;

                    
                    double PriceOffer = PriceSP - (PriceSP *PhanTram_KM/ 100);
                    //if(OfferSP ==null)

                    if (check_start_KM >= 0 && check_end_KM <= 0)
                    {
                        DivHtml = @"<div style='border: 1px solid #000;width: 190px;'>
                              
				                    <table border='0' cellpadding='0' cellspacing='0' style='width: 180px;'>
					                    <tbody>
						                    <tr>
							                    <td colspan='3' style='text-align: center;font-size:15px;font-weight: bold;'>
								                   " + NameSP + @"...
					                            </td>
						                    </tr>
						                    <tr>
							                    <td colspan='3' style='text-align: center;font-size:17px;font-weight: bold;'>
								                   " + string.Format("{0:0,0 VNĐ}", PriceSP) + @"
                                                </td>
						                    </tr>
						                    <tr>
							                    <td colspan='1' rowspan='5' style='text-align: center;width:67px'>
								                    <img src='" + ImgCode + MaVach + @"' style='font-size: 12px; font-weight: normal;' width='67'>
                                                </td>
							
							                    <td colspan='1' rowspan='3' style='text-align: right;'>
								                    <div style='float:right;width:120px'>
                                                    <a style='margin-top: 8px;font-size:12px;font-weight: bold;width:111px;position: absolute;text-align: center;margin-left: 2px;'>" + string.Format("{0:0,0 VNĐ}", PriceOffer) + @"</a>
                                                    <a style='font-size:11px;font-weight: none;position: absolute;margin-top: 24px;width:95px'>" + date_StartKM.ToString("dd/MM") +"-"+ date_StartKM.ToString("dd/MM")+@"</a>
                                                    <img src='http://nhathuoconline.vn/image/data/Sale off/SALE.png' style='width: 120px; height: 63px;' />
                                                    </div>
                                                </td>
						                    </tr>
					                    </tbody>
				                    </table>

				                    </div>";
                    }else
                    {
                       

                        DivHtml = @"<div style='border: 1px solid #000;width: 159px;'>
                              <table cellpadding='0' cellspacing='0' border='0'>
			                        <tbody>
				                        <tr>
					                        <td colspan='3' style='text-align: center;font-size:15px;font-weight: bold;'>
                                                " + NameSP + @"...
					                        </td>
				                        </tr>
				                        <tr>
					                        <td colspan='3' style='text-align: center;font-size:17px;font-weight: bold;'>
				                                " + string.Format("{0:0,0 VNĐ}", PriceSP) + @"
                                            </td>
				                        </tr>
				                        <tr>
					                        <td colspan='2' rowspan='2' style='text-align: left;font-size:17px;font-weight: bold;'>
					                            <img src='" + ImgCode + MaVach + @"' style='font-size: 12px; font-weight: normal;' width='67'>
                                            </td>
					                        <td style='text-align: left;font-size:12px'>
						                        <p style='margin-top: 33px;margin-bottom: -1px;'>" + MaVach +"</br>"+DateTime.Today.ToString("dd/MM/yyyy")+ @"</p>
                                            </td>
				                        </tr>
			                        </tbody>
		                        </table>
                                </div>"; 
                    }
                    
                    int chiadu3 = Convert.ToInt32(ViewState["CountItems"]) % 3;
                    if (chiadu3 == 1)
                    {
                        ltlDivDynamic1.Text = ltlDivDynamic1.Text + DivHtml.ToString();
                    }
                    else if (chiadu3 == 2)
                    {
                        ltlDivDynamic2.Text = ltlDivDynamic2.Text + DivHtml.ToString();
                    }
                    else { 
                        ltlDivDynamic3.Text = ltlDivDynamic3.Text + DivHtml.ToString(); 
                    }
                    txtMaVach.Text = null;
                    txtMaVach.Focus();
                    
                }
                else
                {
                    
                }
            }//end for
            }// end try
            catch (Exception ex)
            {
                ltlError.Text = "<font color='red'>" +  ex.Message + "</font>";
            }
        }
    }
}